package com.yuncheng.spcyApi.selectXzxkApi.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.zwfw.ApiConstant;
import com.yuncheng.spcyApi.entity.SpcyQyJbxx;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.spcyApi.flow.SpcyFlowService;
import com.yuncheng.spcyApi.selectXzxkApi.vo.HqxcjcrwReqVo;
import com.yuncheng.spcyApi.selectXzxkApi.vo.HqxcjcrwResVo;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiSqsxVo;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyQyJbxxService;
import com.yuncheng.spcyApi.service.ISpcySqsxBaiscService;
import com.yuncheng.spcyApi.spcyService.SqsxQywjclService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.HttpProxyUtils;
import com.yuncheng.spcyApi.vo.common.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author hua
 */
@Component(value = "xzspTqApi")
public class XzspTqApi {

    private static final Logger log = LoggerFactory.getLogger(XzspTqApi.class);

    @Resource
    private ApiConstant apiConstant;

    @Resource
    private XzspJkApi xzspJkApi;

    @Resource
    private SqsxQywjclService sqsxQywjclService;

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private ISpcyQyJbxxService spcyQyJbxxService;

    @Resource
    @Lazy
    private SpcyFlowService spcyFlowService;

    @Resource
    private ISpcyLogService spcyLogService;

    /** 指定提取-政务-现场检查任务
     * @param sqdbh 校核码
     *
     * */
    public String appointTqXcjcrw(String sqdbh, String time_stamp){
        String token = xzspJkApi.getToken();//正式使用以后用这个
        if (StringUtils.isBlank(token)){
            return "接口请求token为空";
        }

        HqxcjcrwReqVo hqxcjcrwReqVo = new HqxcjcrwReqVo();
        hqxcjcrwReqVo.setAccess_token(token);

        HqxcjcrwReqVo.Param param = hqxcjcrwReqVo.getParam();
        if (param == null){
            param = hqxcjcrwReqVo.new Param();
        }
        param.setTIME_STAMP(Optional.ofNullable(time_stamp).orElseGet(()->"0"));
        hqxcjcrwReqVo.setParam(param);

        String httpProxyPostResult = HttpProxyUtils.HttpProxyPost(apiConstant.hqxcjcrw_API, JSONObject.toJSONString(hqxcjcrwReqVo));
        if (StringUtils.isBlank(httpProxyPostResult)){
            return "接口请求失败";
        }

        HqxcjcrwResVo hqxcjcrwResVo = JSONObject.parseObject(httpProxyPostResult, HqxcjcrwResVo.class);
        if (hqxcjcrwResVo == null){
            return "数据转换对象为空";
        }

        HqxcjcrwResVo.Data data = hqxcjcrwResVo.getData();
        if (data == null){
            return "转换数据对象为空";
        }

        List<XzspJkApiSqsxVo> list = data.getLIST();
        if (CollectionUtils.isEmpty(list)){
            return "转换数据列表为空";
        }

        for (XzspJkApiSqsxVo demo : list) {

            if (StringUtils.isBlank(demo.getSqdbh())){
                continue;
            }

            if (StringUtils.isNotBlank(sqdbh) && sqdbh.equals(demo.getSqdbh())) {

                SpcySqsxBasic sqsxBasic = spcySqsxBaiscService.selectSqsxById(demo.getId());
                if (sqsxBasic != null){
                    continue;
                }

                SpcySqsxBasic newBaisc = new SpcySqsxBasic();
                String sqsxid = demo.getId();
                newBaisc.setId(sqsxid);
                newBaisc.setFsxsqjbxxid(demo.getSxsqjbxx_id());
                newBaisc.setFsxmc(demo.getSxmc());

                // 转换业务分类编码
                String spcyYwflcode = SpcyConstant.getSpcyYwflcodeByXzspYwfl(demo.getYwfl());
                newBaisc.setFywflcode(spcyYwflcode);

                // 审查模式
                if (spcyYwflcode.equals(SpcyConstant.YWFL_CODE_YLQX)) {
                    // 医疗器械 模式
                    newBaisc.setFscms(SpcyConstant.SQSX_SCMS_SCJCANBJSSC);
                    newBaisc.setFsplx(SpcyConstant.Sp_type_Ns);
                } else if (spcyYwflcode.equals(SpcyConstant.YWFL_CODE_YPZC)) {
                    // 药品注册 模式
                    newBaisc.setFscms(SpcyConstant.SQSX_SCMS_SCJCANBJSSC);
                    newBaisc.setFsplx(SpcyConstant.Sp_type_Ns);
                } else if (spcyYwflcode.equals(SpcyConstant.YWFL_CODE_HZP)) {
                    // 化妆品模式
                    newBaisc.setFscms(SpcyConstant.SQSX_SCMS_SCJCANBJSSC);
                    newBaisc.setFsplx(SpcyConstant.Sp_type_Ns);
                } else if (spcyYwflcode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
                    // 药品生产 模式
                    newBaisc.setFscms(SpcyConstant.SQSX_SCMS_XCJC);
                } else if (spcyYwflcode.equals(SpcyConstant.YWFL_CODE_YPJY)) {
                    // 药品流通 模式
                    newBaisc.setFscms(SpcyConstant.SQSX_SCMS_XCJC);
                }

                newBaisc.setFywflname(SpcyConstant.getYwclNameByCode(spcyYwflcode));
                newBaisc.setFsqsj(demo.getSqsj());
                newBaisc.setFqymc(demo.getQymc());
                newBaisc.setFtyshxydm(demo.getTyshxydm());
                newBaisc.setFqyfddbrfzr(demo.getQyfddbrfzr());
                newBaisc.setFqylxr(demo.getQylxr());
                newBaisc.setFqylxrdh(demo.getQylxrdh());
                newBaisc.setFqyzszcdz(demo.getQyzszcdz());
                newBaisc.setFcpmc(demo.getCpmc());
                newBaisc.setFszdq(demo.getSzdq());
                newBaisc.setFxxdz(demo.getXxdz());
                newBaisc.setFsqdbh(demo.getSqdbh());
                newBaisc.setFspdwmc(demo.getSpdwmc());
                newBaisc.setFbldwmc(demo.getBldwmc());
                newBaisc.setFbldwid(demo.getBldw_id());
                newBaisc.setFblr(demo.getBlr());
                newBaisc.setFblrid(demo.getBlr_id());
                newBaisc.setFfsr(demo.getFsr());
                newBaisc.setFfssj(demo.getFssj());

                if (newBaisc.getFfssj() == null){
                    newBaisc.setFfssj(new Date());
                }
                newBaisc.setFslsj(new Date());

                // 根据当前企业社会信用代码查询，是否存在
                List<SpcyQyJbxx> qy = spcyQyJbxxService.list(new LambdaQueryWrapper<SpcyQyJbxx>()
                        .eq(SpcyQyJbxx::getFtyshxydm, demo.getTyshxydm()));
                if (CollectionUtils.isEmpty(qy)) {
                    // 新增企业基本信息
                    SpcyQyJbxx qyJbxx = new SpcyQyJbxx();
                    String qyId = GetUuIdUtils.NotReplaceUuId();
                    qyJbxx.setId(qyId);
                    qyJbxx.setFqymc(demo.getQymc());
                    qyJbxx.setFtyshxydm(demo.getTyshxydm());
                    qyJbxx.setFqyfddbrfzr(demo.getQyfddbrfzr());
                    qyJbxx.setFqylxr(demo.getQylxr());
                    qyJbxx.setFqylxrdh(demo.getQylxrdh());
                    qyJbxx.setFqyzszcdz(demo.getQyzszcdz());
                    qyJbxx.setFszdq(demo.getSzdq());
                    qyJbxx.setFxxdz(demo.getXxdz());
                    spcyQyJbxxService.save(qyJbxx);

                    String qyLog = "行政审评-提取现场检查数据-新增企业: ";
                    Map<String, Object> qyMap = ObjectToMapUtils.objectToMap(qyJbxx);
                    qyLog += LogUtils.MapToStringMethod(qyMap);
                    spcyLogService.addLogs(LogConstant.LOGS_QY_JBXX, qyLog, sqsxid, LogConstant.LOGS_QY_JBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);

                    newBaisc.setFqyid(qyId);
                } else {
                    newBaisc.setFqyid(qy.get(0).getId());

                }

                newBaisc.setFxzspzt(SpcyConstant.SQSX_BASIC_XZSPZT_YES); // 行政审批数据
                newBaisc.setFsyts("0");
                spcySqsxBaiscService.save(newBaisc);

                /** 初始化流程 */
                Result result = spcyFlowService.initYwflJob(spcyYwflcode, newBaisc.getId(), GetCurrentUserUtils.getCurrentUser(), false);
                if (!result.isSuccess()) {
                    log.error("初始化，流程任务失败-{}", result.getMessage());
                }

                sqsxQywjclService.syncZwxtQysqclAndMlApi(demo.getSbclxxL(), sqsxid, demo.getSxsqjbxx_id());

            }
        }

        return null;
    }

}
